package sctu.edu.java.year2016.team01.xuhanmei.day20160619;

import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2016/6/19.
 */
public class MessyCode {
    public static void main(String[] args) throws UnsupportedEncodingException {
       //1,编码与解码字符集不相同
        //解码
        String s="中国";
        //编码
        byte[] b=s.getBytes();
        //编码与解码字符集相同
        System.out.println(new String(b));

        //查看当前编码字符集
        String encoding = System.getProperty("file.encoding");
        System.out.println("Default System Encoding:" + encoding);

        //设定编码字符集
        b=s.getBytes("gbk");
        //由于编码与解码字符集不同，出现乱码
        System.out.println(new String(b));


        //设置编码字符集和解码字符集相同，则不会出乱码
        byte[] b2="中国".getBytes("gbk");
        s=new String(b2,"gbk");
        System.out.println(s);

        //2,字节数不完整,造成乱码
        String s1="中国";
        byte[] data=s1.getBytes();
        System.out.println(new String(data,0,3));
    }
}
